package com.project.collectAPI.entity.getMeiTuanShopClass;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.collectAPI.comm.JsonParser;
import com.project.collectAPI.comm.SpiderTools;
import com.project.collectAPI.entity.MeiTuan.SystemParam;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/*获取当前 poi_id 店铺的分类*/
@Data
public class getMeiTuanShopClass {

    private final String reqAPI="https://waimaiopen.meituan.com/api/v1/retailCat/list";

    //APP方门店id，即商家中台系统里门店的编码。如商家在操作绑定门店至开放平台应用中时，未绑定三方门店id信息，则默认APP方门店id与美团门店id相同。
    private String app_poi_code;

    //统一的时间戳
    private Long timestamp;

    //签名
    private String signValue;


    /*获取美团类目接口签名*/
    public String getSign() throws Exception {
        SystemParam systemParam=new SystemParam();
        Map<String,Object> map=new HashMap<>();
        map.put("timestamp",timestamp);  //timestamp尽量统一用一次
        map.put("app_id",systemParam.getAppId());
        map.put("app_poi_code",systemParam.getApp_poi_code());
        return systemParam.getSign(map,reqAPI); //获取签名
    }

    public String getShopClassesData(){
        SystemParam systemParam=new SystemParam();
        String reqUrl=reqAPI+"?app_id="+systemParam.getAppId()+"&app_poi_code="+systemParam.getApp_poi_code()+"&timestamp="+timestamp+"&sig="+signValue;
        System.out.println("地址:"+reqUrl);
        String res= SpiderTools.HttpGet(reqUrl);
        return res;
    }

    public JSONArray converClassMap(String ResponseMessage){
        StringBuilder sb=new StringBuilder();
        if(!StringUtils.isEmpty(ResponseMessage)){
            //转换格式为json
            JSONObject jsonObject = JSONObject.parseObject(ResponseMessage);
            JsonParser.traverseJson(jsonObject);
            try { TimeUnit.MILLISECONDS.sleep( 200); } catch (InterruptedException e) { e.printStackTrace(); }
            //遍历对象
            Map<String,Object> getAllJsonObject=JsonParser.getAllJsonObject;
            //StringBuilder DetailImagesList=new StringBuilder();
            JSONArray itemsList= (JSONArray) getAllJsonObject.get("data");
            sb.append("[");
            String rct="";
            for (int i = 0; i < itemsList.size(); i++){
                rct="";
                sb.append("\"");
                JSONObject obj = JSONObject.parseObject(itemsList.get(i).toString());
                String firstClass=obj.getString("name");
                System.out.println("一级分类名称："+firstClass);
                sb.append(firstClass+",");
                JSONArray childrenList= (JSONArray) obj.get("children");
                if(!Objects.isNull(childrenList)){
                    if(childrenList.size()>0){
                        for (int j = 0; j < childrenList.size(); j++){
                            JSONObject sobj = JSONObject.parseObject(childrenList.get(j).toString());
                            String secondClass=sobj.getString("name");
                            System.out.println("################ 二级分类名称："+secondClass);
                            if(j==childrenList.size()-1){
                                sb.append(secondClass);
                            }else{
                                sb.append(secondClass+",");
                            }
                        }
                    }
                }
                if(i==itemsList.size()-1){
                    sb.append("\"");
                }else{
                    sb.append("\",");
                }

                //rct+=sb.toString().substring(0,sb.toString().length()-1);
            }
            sb.append("]");
            //rct=sb.toString().substring(0,sb.toString().length()-1);
            //dataList.add(sb.toString());
        }
        /*System.out.println("=======dataList内容=======");
        dataList.forEach(System.out::println);*/
        //System.out.println(JSONArray.parse(sb.toString()).toString());

       return JSONArray.parseArray(sb.toString());
    }


} //class End
